package mr.ali.webvideo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mr.ali.webvideo.model.CollectionPlay;
import mr.ali.webvideo.model.CollectionPlayCllect;
import mr.ali.webvideo.mapper.CollectionPlayCllectMapper;
import mr.ali.webvideo.service.ICollectionPlayCllectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mr.ali.webvideo.service.ICollectionPlayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-16
 */
@Service
public class CollectionPlayCllectServiceImpl extends ServiceImpl<CollectionPlayCllectMapper, CollectionPlayCllect> implements ICollectionPlayCllectService {

    @Resource
    private CollectionPlayCllectMapper collectionPlayCllectMapper;

    @Resource
    private ICollectionPlayService collectionPlayService;

    @Override
    public PageInfo<CollectionPlayCllect> findAll(Integer pageNum, Integer pageSize, Integer userId, Integer videoId) {
        QueryWrapper<CollectionPlayCllect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        if (videoId != null) {
            queryWrapper.eq("video_id", videoId);
        }
        queryWrapper.orderByDesc("gmt_create");
        PageHelper.startPage(pageNum, pageSize);
        List<CollectionPlayCllect> tags = collectionPlayCllectMapper.selectList(queryWrapper);
        for (CollectionPlayCllect video : tags) {
            List<CollectionPlay> videos = collectionPlayService.videoTagsById(video.getVideoId());
            video.setCollection(videos);
        }
        return new PageInfo<>(tags);
    }

    @Override
    public Integer delete(Integer userId, Integer urlId) {
        QueryWrapper<CollectionPlayCllect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("url_id", urlId);
        return collectionPlayCllectMapper.delete(queryWrapper);
    }

    /**
     * 计算点赞数
     */
    @Override
    public Long CountPlayCommentIndex(Integer urlId) {
        QueryWrapper<CollectionPlayCllect> wrapper = new QueryWrapper<>();
        wrapper.eq("url_id", urlId);
        Long row = collectionPlayCllectMapper.selectCount(wrapper);
        return row;
    }

    @Override
    public Boolean SelectNewCollect(Integer userId, Integer urlId) {
        QueryWrapper<CollectionPlayCllect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("url_id", urlId);
        Long row = collectionPlayCllectMapper.selectCount(queryWrapper);
        return row > 0;
    }

    @Override
    public Integer insert(Integer userId, Integer videoId, Integer urlId, Integer Episode) {
        CollectionPlayCllect collectionCllect = new CollectionPlayCllect();
        collectionCllect.setUserId(userId);
        collectionCllect.setEpisode(Episode);
        collectionCllect.setVideoId(videoId);
        collectionCllect.setUrlId(urlId);
        collectionCllect.setGmtCreate(System.currentTimeMillis());
        return   collectionPlayCllectMapper.insert(collectionCllect);
    }
}
